<?php

//
// The class that displays images (icons and thumbnails)
//
class ImageServer {

    //
    // Checks if an image is requested and displays one if needed
    //
	public static function showImage() {
        global $_IMAGES;
        if (isset($_GET['img'])) {
            if (strlen($_GET['img']) > 0) {
                $mtime = gmdate('r', filemtime($_SERVER['SCRIPT_FILENAME']));
                $etag = md5($mtime . $_SERVER['SCRIPT_FILENAME']);

                if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $mtime) || (isset($_SERVER['HTTP_IF_NONE_MATCH']) && str_replace('"', '', stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == $etag)) {
                    header('HTTP/1.1 304 Not Modified');
                    return true;
                } else {
                    header('ETag: "' . $etag . '"');
                    header('Last-Modified: ' . $mtime);
                    header('Content-type: image/gif');
                    if (isset($_IMAGES[$_GET['img']]))
                        print base64_decode($_IMAGES[$_GET['img']]);
                    else
                        print base64_decode($_IMAGES["unknown"]);
                }
            }
            return true;
        }
        else if (isset($_GET['thumb'])) {
            if (strlen($_GET['thumb']) > 0 && EncodeExplorer::getConfig('thumbnails') == true) {
                ImageServer::showThumbnail($_GET['thumb']);
            }
            return true;
        }
        return false;
    }

    public static function isEnabledPdf() {
        if (class_exists("Imagick"))
            return true;
        return false;
    }

    public static function openPdf($file) {
        if (!ImageServer::isEnabledPdf())
            return null;

        $im = new Imagick($file . '[0]');
        $im->setImageFormat("png");
        $str = $im->getImageBlob();
        $im2 = imagecreatefromstring($str);
        return $im2;
    }

    //
    // Creates and returns a thumbnail image object from an image file
    //
	public static function createThumbnail($file) {
        if (is_int(EncodeExplorer::getConfig('thumbnails_width')))
            $max_width = EncodeExplorer::getConfig('thumbnails_width');
        else
            $max_width = 200;

        if (is_int(EncodeExplorer::getConfig('thumbnails_height')))
            $max_height = EncodeExplorer::getConfig('thumbnails_height');
        else
            $max_height = 200;

        if (File::isPdfFile($file))
            $image = ImageServer::openPdf($file);
        else
            $image = ImageServer::openImage($file);
        if ($image == null)
            return;

        imagealphablending($image, true);
        imagesavealpha($image, true);

        $width = imagesx($image);
        $height = imagesy($image);

        $new_width = $max_width;
        $new_height = $max_height;
        if (($width / $height) > ($new_width / $new_height))
            $new_height = $new_width * ($height / $width);
        else
            $new_width = $new_height * ($width / $height);

        if ($new_width >= $width && $new_height >= $height) {
            $new_width = $width;
            $new_height = $height;
        }

        $new_image = ImageCreateTrueColor($new_width, $new_height);
        imagealphablending($new_image, true);
        imagesavealpha($new_image, true);
        $trans_colour = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
        imagefill($new_image, 0, 0, $trans_colour);

        imagecopyResampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

        return $new_image;
    }

    //
    // Function for displaying the thumbnail.
    // Includes attempts at cacheing it so that generation is minimised.
    //
	public static function showThumbnail($file) {
        if (filemtime($file) < filemtime($_SERVER['SCRIPT_FILENAME']))
            $mtime = gmdate('r', filemtime($_SERVER['SCRIPT_FILENAME']));
        else
            $mtime = gmdate('r', filemtime($file));

        $etag = md5($mtime . $file);

        if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $mtime) || (isset($_SERVER['HTTP_IF_NONE_MATCH']) && str_replace('"', '', stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == $etag)) {
            header('HTTP/1.1 304 Not Modified');
            return;
        } else {
            header('ETag: "' . $etag . '"');
            header('Last-Modified: ' . $mtime);
            header('Content-Type: image/png');
            $image = ImageServer::createThumbnail($file);
            imagepng($image);
        }
    }

    //
    // A helping function for opening different types of image files
    //
	public static function openImage($file) {
        $size = getimagesize($file);
        switch ($size["mime"]) {
            case "image/jpeg":
                $im = imagecreatefromjpeg($file);
                break;
            case "image/gif":
                $im = imagecreatefromgif($file);
                break;
            case "image/png":
                $im = imagecreatefrompng($file);
                break;
            default:
                $im = null;
                break;
        }
        return $im;
    }

}

?>
